using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Framework.Library.Components.Modules;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Library;
using Framework.Library.Components.Commands;

namespace Portal.Administrator.Modules
{
	public partial class ModuleDefinition : AuthenticatedControl
	{
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            lblCommandName.Text = AppEnv.CommandName();
        }

        protected override void OnPreRender(EventArgs e)
        {            
            DataTable dtModules = ModuleController.GetModules();
            LoadForTreeView(dtModules);
            LoadData();
        }

        private void LoadData()
        {
            if (txtID.Text != string.Empty)
            {
                ModuleInfo curModule = ModuleController.GetModule(ConvertUtility.ToInt32(txtID.Text));
                if (curModule != null)
                {
                    txtID.Text = curModule.ModuleID.ToString();
                    txtModuleName.Text = curModule.ModuleName;
                    txtModuleTitle.Text = curModule.ModuleTitle;
                    txtModuleDescription.Text = curModule.ModuleDescription;
                    txtModuleFolder.Text = curModule.ModuleFolder;
                }
            }
        }

        private void LoadForTreeView(DataTable dtModules)
        {
            string nodePath = "|";
            TreeNode focusNode = tvwModules.SelectedNode;
            if (focusNode != null)
            {
                while (true)
                {
                    if (focusNode.Parent == null) break;
                    else
                    {
                        focusNode = focusNode.Parent;
                        nodePath += focusNode.Value + "|";
                    }
                }
            }
            tvwModules.Nodes.Clear();

            TreeNode topRoot = new TreeNode();
            topRoot.Text = "Root";
            topRoot.Value = "0";
            tvwModules.Nodes.Add(topRoot);

            foreach (DataRow rows in dtModules.Rows)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = rows["ModuleName"].ToString();
                rootNode.Value = rows["ModuleID"].ToString();
                rootNode.Expanded = false;
                tvwModules.Nodes.Add(rootNode);
            }
        }
        protected void tvwModules_SelectedNodeChanged(object sender, EventArgs e)
        {
            txtID.Text = tvwModules.SelectedNode.Value;
            lblUpdateStatus.Text = string.Empty;
        } 

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                ModuleInfo newmodule = new ModuleInfo();
                newmodule.ModuleName = txtModuleName.Text.Trim();
                newmodule.ModuleTitle = txtModuleTitle.Text.Trim();
                newmodule.ModuleDescription = txtModuleDescription.Text.Trim();
                newmodule.ModuleFolder = txtModuleFolder.Text.Trim();
                txtID.Text = ModuleController.AddModule(newmodule).ToString();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid) return;
            ModuleInfo module = new ModuleInfo();
            if (module == null) return;
            module.ModuleID = Convert.ToInt32(txtID.Text);
            module.ModuleName = txtModuleName.Text.Trim();
            module.ModuleTitle = txtModuleTitle.Text.Trim();
            module.ModuleDescription = txtModuleDescription.Text.Trim();
            module.ModuleFolder = txtModuleFolder.Text.Trim();
            try
            {
                ModuleController.UpdateModule(module);
                this.lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtID.Text) || Convert.ToInt32(txtID.Text) == 0)
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_ERROR;
            else
            {
                try
                {
                    ModuleController.DeleteModule(Convert.ToInt32(txtID.Text));
                    lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                }
                catch (Exception ex)
                {
                    lblUpdateStatus.Text = ex.Message;
                }
            }            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtID.Text = string.Empty;
            this.txtModuleDescription.Text = string.Empty;
            this.txtModuleFolder.Text = string.Empty;
            this.txtModuleName.Text = string.Empty;
            this.txtModuleTitle.Text = string.Empty;
        }
	}
}